<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/4/19
 * Time: 11:02
 */

namespace app\api\controller;


use think\Db;

class Chat extends Base
{
    public $user_id;
    public $create_time;
    public $page;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 单聊用户列表
     * @return \think\response\Json
     * type   1 我的上级   2 我的下级
     */
    public function singleChatList(){
        $type = input('type');
        $user_invite = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_invite, user_invite_other')
            ->find();
    
        $chatUserList = Db::table('up_u_user')
            ->where('user_role', 'IN', '2,3')
            ->where('user_status', 1)
            ->field('user_id, user_username, user_image, user_phone');
        if($type == 1){
            if(!$user_invite['user_invite_other']) return json(['status' => 200, 'msg' => []]);
            $chatUserList->where(['user_invite' => [['=', $user_invite['user_invite_other']], ['NEQ', '0'], 'and']]);
        }else{
            if(!$user_invite['user_invite']) return json(['status' => 200, 'msg' => []]);
            $chatUserList->where('user_invite_other', $user_invite['user_invite']);
        }
        $chatUserList = $chatUserList->select();
        if($chatUserList){
            foreach($chatUserList as $k => $v){
                // 字母排序
                $chatUserList[$k]['letter'] = $this->Getzimu($chatUserList[$k]['user_username']);
            }
            $new_names = array_column($chatUserList, 'letter');
            array_multisort($new_names, SORT_ASC, $chatUserList);
            $start = '';
            $res = array();
            foreach ($chatUserList as $k => $v) {
                if ($v['letter'] != $start) {
                    $start = $v['letter'];
                }
                $res[$start][] = $v;
            }
            $jing = array();
            if(isset($res['#'])){
                $jing['#'] = $res['#'];
                unset($res['#']);
            }
            $chatUserList = array_merge($res,$jing);
            return json(['status' => 200, 'msg' => $chatUserList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
//        if(!$user_invite['user_invite'] && !$user_invite['user_invite_other']){
//            return json(['status' => 200, 'msg' => []]);
//        }else{
//            $chatUserList = Db::table('up_u_user')
//                ->where('user_role', 'IN', '2,3')
//                ->where('user_status', 1)
//                ->field('user_id, user_username, user_image, user_phone');
//            if($type == 1){
//                $chatUserList->where(['user_invite' => [['=', $user_invite['user_invite_other']], ['NEQ', ''], 'and']]);
//            }else{
//                $chatUserList->where('user_invite_other', $user_invite['user_invite']);
//            }
//            $chatUserList = $chatUserList->select();
//            if($chatUserList){
//                foreach($chatUserList as $k => $v){
//                    // 字母排序
//                    $chatUserList[$k]['letter'] = $this->Getzimu($chatUserList[$k]['user_username']);
//                }
//                $new_names = array_column($chatUserList, 'letter');
//                array_multisort($new_names, SORT_ASC, $chatUserList);
//                $start = '';
//                $res = array();
//                foreach ($chatUserList as $k => $v) {
//                    if ($v['letter'] != $start) {
//                        $start = $v['letter'];
//                    }
//                    $res[$start][] = $v;
//                }
//                $jing = array();
//                if(isset($res['#'])){
//                    $jing['#'] = $res['#'];
//                    unset($res['#']);
//                }
//                $chatUserList = array_merge($res,$jing);
//                return json(['status' => 200, 'msg' => $chatUserList]);
//            }else{
//                return json(['status' => 200, 'msg' => []]);
//            }
//        }
        
    }
    
    /**
     * 聊天用户基本信息
     * @return \think\response\Json
     * user_ids     用户ID,逗号隔开
     */
    public function chatUserInfo(){
        $user_ids = input('user_ids');
        $user = Db::table('up_u_user')
            ->where('user_id', 'IN', $user_ids)
            ->field('user_id, user_username, user_image')
            ->select();
        return json(['status' => 200, 'msg' => $user]);
    }
    
    /**
     * 保存聊天记录
     * @return \think\response\Json
     * receive_user_id    接收消息用户ID
     * chat_content       消息内容
     * chat_type          消息内容    1 文字   2 图片   3 视频   4 音频
     */
    public function chatRecord(){
        $receive_user_id = input('receive_user_id');
        $chat_content = input('chat_content');
        $chat_type = input('chat_type'); // 1 文字   2 图片   3 视频   4 音频
        $send_user_phone = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_phone');
        $receive_user_phone = Db::table('up_u_user')->where('user_id', $receive_user_id)->value('user_phone');
        
        $insert = [
            'chat_send_user_id' => $this->user_id,
            'chat_send_phone' => $send_user_phone,
            'chat_receive_user_id' => $receive_user_id,
            'chat_receive_phone' => $receive_user_phone,
//            'chat_content' => $chat_content,
            'chat_type' => $chat_type,
            'chat_time' => $this->create_time
        ];
        if($chat_type == 4){ // 音频需要转码
            $arr = explode('/', $chat_content);
            $key = end($arr);
            $this->audioZhuanma($key);
            $audio_url = config('qiniu.domain') . 'audio_' . $key;
            $insert['chat_content'] = $audio_url;
        }else{
            $insert['chat_content'] = $chat_content;
        }
        $res = Db::table('up_user_chat')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => false]);
        }
    }
    
    /**
     * 首字母
     * @param $str
     * @return string
     */
    public function Getzimu($str)
    {
        if(empty($str)){
            return '#';
        }
        $fchar = mb_substr($str, 0, 1, 'utf-8');
        // 判断表情    表情长度为4位
        if(strlen($fchar) >= 4 ){
            return '#';
        }
//        print_r(ord('z'));die;
//        $fchar=ord($str{0});
        if(ord($fchar) >= ord('A') && ord($fchar) <= ord('z')){
            return strtoupper($fchar);
        }
        $s1 = iconv('UTF-8', 'gb2312', $str);
        $s2 = iconv('gb2312', 'UTF-8', $s1);
        $s = $s2 == $str ? $s1 : $str;
        $asc = ord($s{0})*256+ord($s{1})-65536;
        if($asc >= -20319 && $asc <= -20284) return 'A';
        if($asc >= -20283 && $asc <= -19776) return 'B';
        if($asc >= -19775 && $asc <= -19219) return 'C';
        if($asc >= -19218 && $asc <= -18711) return 'D';
        if($asc >= -18710 && $asc <= -18527) return 'E';
        if($asc >= -18526 && $asc <= -18240) return 'F';
        if($asc >= -18239 && $asc <= -17923) return 'G';
        if($asc >= -17922 && $asc <= -17418) return 'H';
        if($asc >= -17417 && $asc <= -16475) return 'J';
        if($asc >= -16474 && $asc <= -16213) return 'K';
        if($asc >= -16212 && $asc <= -15641) return 'L';
        if($asc >= -15640 && $asc <= -15166) return 'M';
        if($asc >= -15165 && $asc <= -14923) return 'N';
        if($asc >= -14922 && $asc <= -14915) return 'O';
        if($asc >= -14914 && $asc <= -14631) return 'P';
        if($asc >= -14630 && $asc <= -14150) return 'Q';
        if($asc >= -14149 && $asc <= -14091) return 'R';
        if($asc >= -14090 && $asc <= -13319) return 'S';
        if($asc >= -13318 && $asc <= -12839) return 'T';
        if($asc >= -12838 && $asc <= -12557) return 'W';
        if($asc >= -12556 && $asc <= -11848) return 'X';
        if($asc >= -11847 && $asc <= -11056) return 'Y';
        if($asc >= -11055 && $asc <= -10247) return 'Z';
        return "#";
    }
}